﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    Layout = null;
}
<link href="~/lib/layui/css/layui.css" rel="stylesheet" />
<script src="~/lib/layui/layui.js"></script>
<script src="~/js/js.cookie.js"></script>
<script src="~/js/moment.js"></script>
<script src="~/lib/jquery/dist/jquery.js"></script>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->
    <link href="//unpkg.com/layui@2.9.20/dist/css/layui.css" rel="stylesheet">
</head>
<body>
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">资源</label>
            <div class="layui-input-block">
                <select name="resourceName" lay-filter="aihao" id="resourceName">
                    <option value="" selected>请选择</option>
                    <option value="测试人员">测试人员</option>
                    <option value="测试路由器">测试路由器</option>
                    <option value="测试仪器">测试仪器</option>
                    <option value="机床测试">机床测试</option>
                    <option value="轮胎测试">轮胎测试</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">可用数量</label>
            <div class="layui-input-block">
                <input type="text" id="resourceNumber" name="resourceNumber" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">摘要</label>
            <div class="layui-input-block">
                <input type="text" id="abstract" name="abstract" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" pane>
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <select name="resourceState" lay-filter="aihao" id="resourceState">
                    <option value="0" selected>请选择</option>
                    <option value="1">有效</option>
                    <option value="2">无效</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" id="component" class="layui-btn layui-btn-normal" lay-submit lay-filter="demo-val">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</body>
</html>

<script>
    var $ = layui.$;
    var form = layui.form;
    // 使用layui的layer模块
    layui.use(['form', 'layer'], function () {
        console.log(location.search.split("=")[1])

        // 表单取值
        layui.$('#component').on('click', function () {
            var data = form.val('demo-val-filter');
        });

        // 资源添加
        form.on('submit(demo-val)', function (data) {
            var field = data.field; // 获取表单字段值
            field.createdOwner = localStorage.getItem('userName');
            field.workCenterCode = location.search.split("=")[1];
            console.log(field) //在控制台显示值
            // 此处可执行 Ajax 等操作
            $.ajax({
                type: "post",
                url: '@ViewBag.RedDragonWrite' + "/api/Resource/CreateResource",
                data: JSON.stringify(field),
                dataType: "json",
                contentType: "application/json",
                success: res => {
                    // 处理响应，例如提示成功信息
                    layui.layer.msg(res.msg, { icon: 6, time: 1000 });
                    // 使用setTimeout来延迟关闭操作
                    setTimeout(function () {
                        // 操作成功后关闭当前弹出层
                        var index = parent.layer.getFrameIndex(window.name); // 获取当前弹出层的索引
                        parent.layer.close(index); // 关闭弹出层
                    }, 2000);
                },
                error: function (xhr, status, error) {
                    // 处理错误
                    layui.layer.msg('添加失败', { icon: 5, time: 1000 });
                }
            })
            return false; // 阻止默认 form 跳转
        });
    });

</script>
